package com.sixth.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.SubjectDao;
import com.sixth.entity.Course;
import com.sixth.entity.EduTeacher;
import com.sixth.entity.Subject;
import com.sixth.service.SubjectService;
import com.sixth.until.CommonResult;
import com.sixth.until.OrderNoUtil;
import com.sixth.until.TableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: 老魏
 * @create: 2022-04-18 11:06
 **/
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectDao subjectDao;

    @Override
    public TableResult findSubject(Integer pageCurrent, Integer pageSize ,Subject subject){
        PageHelper.startPage(pageCurrent,pageSize);
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        if (subject.getTitle()!=null && subject.getTitle()!=""){
            wrapper.eq("title",subject.getTitle());
        }
        if (subject.getStatusId()!=null &&subject.getStatusId()!=""){
            wrapper.eq("status_id",subject.getStatusId());
        }
        List<Subject> subjects = subjectDao.selectList(wrapper);

        if (subjects.size()!=0){
            QueryWrapper<Subject> wrapper1 = new QueryWrapper<>();
            wrapper1.ne("parent_id","0");
            List<Subject> selectList = subjectDao.selectList(wrapper1);

            //递归调用
            for (Subject one : subjects) {
                List<Subject> children = findChildren(selectList,one.getId());
                one.setChildren(children);
            }
            PageInfo<Subject> pageInfo = new PageInfo<>(subjects);
            return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
        }else {
            QueryWrapper<Subject> wrapper1 = new QueryWrapper<>();
            if (subject.getTitle()!=null && subject.getTitle()!=""){
                wrapper1.eq("title",subject.getTitle());
            }
            if (subject.getStatusId()!=null &&subject.getStatusId()!=""){
                wrapper1.eq("status_id",subject.getStatusId());
            }
            List<Subject> subjects1 = subjectDao.selectList(wrapper1);
            QueryWrapper<Subject> wrapper2 = new QueryWrapper<>();
            wrapper1.ne("parent_id","0");
            List<Subject> selectList = subjectDao.selectList(wrapper1);
            //递归调用
            for (Subject one : subjects1) {
                List<Subject> children = findChildren(selectList,one.getId());
                one.setChildren(children);
            }
            PageInfo<Subject> pageInfo = new PageInfo<>(subjects1);
            return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
        }
    }
    List<Subject> findChildren(List<Subject> subjectList,String fatherid){
        ArrayList<Subject> children = new ArrayList<>();
        for (Subject subject : subjectList) {
            if (subject.getParentId().equals(fatherid)){
                children.add(subject);
            }
        }
        for (Subject c : children) {
            List<Subject> children1 = findChildren(subjectList,c.getId());
            c.setChildren(children1);
        }
        return children;
    }

    //修改状态
    @Override
    public CommonResult changeStatusId(String id ) {
        System.out.println(id);
        Subject subject1 = subjectDao.selectById(id);
        System.out.println(subject1);
        if (subject1.getStatusId().equals("1")){
            Subject subject = new Subject();
            subject.setId(id);
            subject.setStatusId("0");
            subjectDao.updateById(subject);
            subjectDao.updateByparentId(id);
            return new CommonResult(2000,"状态修改成功",null);
        }else {
            Subject subject = new Subject();
            subject.setId(id);
            subject.setStatusId("1");
            subjectDao.updateById(subject);
            return new CommonResult(2000,"状态修改成功",null);
        }
    }

    //操作编辑
    @Override
    public CommonResult updateSubject(Subject subject) {
        String id = subject.getId();
        String title = subject.getTitle();
        String remark = subject.getRemark();
        System.out.println(id+title+remark);
        int i = subjectDao.updatesubject(id,title,remark);
        return new CommonResult(2000,"修改成功",null);
    }
    //操作删除
    @Override
    public CommonResult deleteSubject(String id){
        subjectDao.deleteById(id);
        return  new CommonResult(2000,"删除成功",null);
    }
    //操作添加
    @Override
    public CommonResult insertSubject(Subject subject) {
        String id1 = subject.getId();
        String parentId = subject.getParentId();
        String title = subject.getTitle();
        String remark = subject.getRemark();
        Date date = new Date();
        Date date1 = new Date();
        String id = OrderNoUtil.getOrderNo();
        System.out.println(parentId+"=====");
        System.out.println(subject.getId()+"====");
        if (parentId=="0"&&id1=="0"){
            subjectDao.insertSubject(id,parentId,title,remark,date,date1);
            return new CommonResult(2000,"新增成功",null);
        }
        parentId = id1;
        //String parentId1 = subject.getParentId();
        //System.out.println(parentId + "===");
        subjectDao.insertSubject(id, parentId, title, remark, date, date1);
        return new CommonResult(2000, "新增成功", null);
    }

}
